﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 待办事宜清单 数据类
    /// </summary>
    public class ApsToDoList
    {
        public ApsToDoList()
        {
        }

        /// <summary>
        /// 获取个人待办事项列表
        /// <creator>wangl</creator>
        /// <date>2013-08-26</date>
        /// </summary>
        /// <param name="logId">登录ID</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>待办事项列表</returns>
        public List<Model.ApsToDoList> GetApsToDoList(string logId, string entityNo)
        {
            List<Model.ApsToDoList> apsToDoList = new List<Model.ApsToDoList>();
            DAL.PrvUser prvUserDAL = new PrvUser();
            DAL.HREmpl hrEmplDAL = new HREmpl();
            Model.PrvUser prvUserModel = prvUserDAL.GetModel(logId, entityNo);
            if (prvUserModel != null)
            {
                Model.ApsToDoParams apsTodoParamsModel = new Model.ApsToDoParams();
                apsTodoParamsModel.EmpID = prvUserModel.FEmplNo;
                apsTodoParamsModel.EntityNo = entityNo;
                apsTodoParamsModel.LogID = logId;
                string emplNo = prvUserModel.FEmplNo;
              

                DataSet ds = hrEmplDAL.GetEmplFromViewByEmplNo(emplNo, entityNo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataTable tmpDT = ds.Tables[0];
                    for (int i = 0; i < tmpDT.Rows.Count; i++)
                    {
                        if (tmpDT.Rows[i]["FIsMainPosition"].ToString() == "Y")
                        {
                            apsTodoParamsModel.DeptID = tmpDT.Rows[i]["FDeptNo"].ToString();
                            break;
                        }

                    }
                }
               

                //获取有菜单权限的提醒列表
                StringBuilder strSql = new StringBuilder();
                if (logId == "admin")
                {
                    strSql = new StringBuilder();
                    strSql.Append("SELECT FToDoListID,FSQLSyntax,FMessage,FFunctURL,FSeq,FIsNewWindow,FFunctNo ");
                    strSql.Append(" FROM dbo.t_apsToDoList");
                    //strSql.Append(" WHERE FFunctNo IS NOT NULL");
                    strSql.Append(" ORDER BY FSeq ");
                    ds = DbHelperSQL.Query(strSql.ToString());
                }
                else
                {
                    strSql.Append("SELECT c.FToDoListID,c.FSQLSyntax,c.FMessage,c.FFunctURL,c.FSeq,c.FIsNewWindow,c.FFunctNo ");
                    strSql.Append(" FROM dbo.t_PrvGroupPriv a ,dbo.t_PrvUserGroup b ,dbo.t_apsToDoList c");
                    strSql.Append(" WHERE a.FGroupNo = b.FGroupNo AND c.FFunctNo = a.FFunctNo AND c.FFunctNo IS NOT NULL");
                    strSql.Append(" AND (b.FLogID=@FLogID)");

                    strSql.Append(" UNION ");
                    strSql.Append("SELECT FToDoListID,FSQLSyntax,FMessage,FFunctURL,FSeq,FIsNewWindow,FFunctNo");
                    strSql.Append(" FROM dbo.t_apsToDoList");
                    strSql.Append("    WHERE FFunctNo IS NULL");
                    strSql.Append("    ORDER BY FSeq ");
                    SqlParameter[] parameters = { 
                 new SqlParameter("@FLogID", SqlDbType.NVarChar,30)
                                        };
                    parameters[0].Value = logId;

                    ds = DbHelperSQL.Query(strSql.ToString(), parameters);
                }
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    string sqlSyntax = dr["FSQLSyntax"].ToString();
                    strSql = new StringBuilder();
                    sqlSyntax = sqlSyntax.ToUpper();
                    sqlSyntax = sqlSyntax.Replace("{LOGID}", apsTodoParamsModel.LogID);
                    sqlSyntax = sqlSyntax.Replace("{EMPID}", apsTodoParamsModel.EmpID);
                    sqlSyntax = sqlSyntax.Replace("{DEPTID}", apsTodoParamsModel.DeptID);
                    sqlSyntax = sqlSyntax.Replace("{ENTITYNO}", apsTodoParamsModel.EntityNo);
                    strSql.Append(sqlSyntax);
                    object obj = DbHelperSQL.GetSingle(strSql.ToString());
                    int count = 0;
                    if (obj != null)
                    {
                        count = Convert.ToInt32(obj);
                    }
                    if (count > 0)
                    {
                        sqlSyntax = dr["FMessage"].ToString();
                        sqlSyntax = sqlSyntax.Replace("{COUNT}", count.ToString());
                        dr["FMessage"] = sqlSyntax;
                        Model.ApsToDoList apsToDoListModel = DataRowToModel(dr);
                        apsToDoList.Add(apsToDoListModel);
                    }

                }


                ////获取不限菜单权限的提醒列表
                //strSql = new StringBuilder();
                //strSql.Append("SELECT FToDoListID,FSQLSyntax,FMessage,FFunctURL,FSeq,FIsNewWindow,FFunctNo ");
                //strSql.Append(" FROM dbo.t_apsToDoList");
                //strSql.Append(" WHERE FFunctNo IS NULL");

                //ds = DbHelperSQL.Query(strSql.ToString());
                //dt = ds.Tables[0];
            }
            return apsToDoList;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ApsToDoList DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ApsToDoList model = new ElingOA6_Admin_DSJCJ.Model.ApsToDoList();
            if (row != null)
            {
                if (row["FToDoListID"] != null && row["FToDoListID"].ToString() != "")
                {
                    model.FToDoListID = decimal.Parse(row["FToDoListID"].ToString());
                }
                if (row["FSQLSyntax"] != null)
                {
                    model.FSQLSyntax = row["FSQLSyntax"].ToString();
                }
                if (row["FMessage"] != null)
                {
                    model.FMessage = row["FMessage"].ToString();
                }
                if (row["FFunctURL"] != null)
                {
                    model.FFunctURL = row["FFunctURL"].ToString();
                }
                if (row["FSeq"] != null && row["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(row["FSeq"].ToString());
                }
                if (row["FIsNewWindow"] != null)
                {
                    model.FIsNewWindow = row["FIsNewWindow"].ToString();
                }
                if (row["FFunctNo"] != null)
                {
                    model.FFunctNo = row["FFunctNo"].ToString();
                }
            }
            return model;
        }

    }
}
